#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    # import pymel.core as pm
    import JTK.jmaya.core as jmc

except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckHiddenNodes(CheckBase):
    def __init__(self):
        super(CheckHiddenNodes, self).__init__()
        pass

    def get_sub_nodes(self, parent, sub_nodes=[]):
        if cmds.listRelatives(parent, c=1, f=1):
            for sub_node in cmds.listRelatives(parent, c=1, f=1):
                sub_nodes.append(sub_node)
                if cmds.listRelatives(sub_node, c=1, f=1):
                    self.get_sub_nodes(sub_node, sub_nodes)

    def get_hidden_nodes(self):
        hidden_nodes = []
        for node in cmds.ls(assemblies=1, l=1):
            if cmds.getAttr(node + ".hiddenInOutliner"):
                hidden_nodes.append(node)
            sub_nodes = []
            self.get_sub_nodes(node, sub_nodes)
            for sub_node in sub_nodes:
                if cmds.getAttr(sub_node + ".hiddenInOutliner"):
                    hidden_nodes.append(sub_node)
        return hidden_nodes

    def check(self):
        result_proc = self.get_hidden_nodes()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        result_proc = self.get_hidden_nodes()
        try:
            for node in result_proc:
                cmds.setAttr(node + ".hiddenInOutliner", False)
            return self.check()
        except:
            return self.check()
